/**
 * 
 */
package com.e5.training.action;

import org.apache.struts2.json.annotations.JSON;
import org.springframework.beans.factory.annotation.Autowired;

import com.e5.training.domain.User;
import com.e5.training.service.UserService;
import com.opensymphony.xwork2.ActionContext;

/**
 * @author colin
 * 
 */
public class LoginAction {
	public String userId;
	public String password;
	private boolean isValid;
	
	@Autowired
	private UserService userService;
    
	/**
	 * @return the userId
	 */
	@JSON(serialize = false)
	public String getUserId() {
		return userId;
	}

	
	/**
	 * @param userId
	 *            the userId to set
	 */
	public void setUserId(String userId) {
		this.userId = userId;
	}

	/**
	 * @return the password
	 */
	@JSON(serialize = false)
	public String getPassword() {
		return password;
	}

	/**
	 * @param password
	 *            the password to set
	 */
	public void setPassword(String password) {
		this.password = password;
	}
	@JSON(name="isValid")
	public boolean getIsValid() {
		return isValid;
	}

	public void setIsValid(boolean isvalid) {
		this.isValid = isvalid;
	}

	public String execute() {
		String userid = this.getUserId();
		String password = this.getPassword();
		try {
			boolean isvalid = false;
			User user = userService.login(userid, password);
			if (user == null) {
				isvalid = false;
			} else {
				isvalid = true;
				ActionContext.getContext().getSession()
						.put("accountInfo", user);
			}
			this.setIsValid(isvalid);
		} catch (Exception ex) {
			return null;
		}
		return "success";
	}
}
